﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PitOfDespair.Dungeon
{
   
    class FreeForm : IGenerationStrategy
    {
        public FreeForm(int height, int width, Random seed)
        {
            rnd = seed;
            this.width = width;
            this.height = height;
        }
        readonly Random rnd;
        private int width;
        private int height;
        #region IGenerationStrategy Members

        WallType GetNextWall()
        {
            var wall = rnd.NextDouble() < .7 ? WallType.Open : WallType.SolidWall;

            return wall;
        }
        public Tuple<Location[,],int,int> Generate()
        {
            var x = rnd.Next(this.width);
            var y = rnd.Next(this.height);
            return Tuple.Create(Generate(x,y ),x,y);
        }
        public Location[,] Generate(int startX, int startY)
        {
            var loc = new Location[10,10];
            for (int x = 0; x < 10; x++)
                for (int y = 0; y < 10; y++)
                {
                    var north = y == 0 ? WallType.SolidWall : GetNextWall();
                    var south = y == loc.GetUpperBound(1) -1 ? WallType.SolidWall : GetNextWall();
                    var west = x == 0 ? WallType.SolidWall : GetNextWall();
                    var east = x == loc.GetUpperBound(0) -1 ? WallType.SolidWall : GetNextWall();
                    
                    loc[x, y] = new Location(west,east,north,south);
                }
            loc = ConnectClosedLocations(loc);

            return loc;

        }

        private Location[,] ConnectClosedLocations(Location[,] loc)
        {
            //TODO: add doors to make all rooms connected
            return loc;
        }

        #endregion

        
    }
}
